package com.lexin.action.controller.service;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.lexin.action.controller.BaseController;
import com.lexin.action.service.UpLoadAndDownLoadService;
import com.lexin.common.model.base.SystemProperties;
import com.lexin.common.model.state.DownloadState;
import com.lexin.common.model.state.FileState;
import com.lexin.common.model.table.FileInfo;
import com.lexin.common.model.utils.FormBeanUtil;
import com.lexin.common.model.utils.ItemFilterUtil;

@Controller
@RequestMapping("/service/upload_download")
public class UpLoadAndDownLoadSController extends BaseController {

	@Autowired
	UpLoadAndDownLoadService upLoadAndDownLoadService;
	
	@RequestMapping("/upload")
	public ModelAndView upload( HttpServletRequest request, HttpServletResponse response ) throws Exception {
		Map<String, String> message = upLoadAndDownLoadService.upload(request);
		response.setCharacterEncoding(SystemProperties.ENCODING);
		response.getWriter().print( JSONObject.wrap(message) );
		return null;
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping("/show")
	public ModelAndView show( HttpServletRequest request, HttpServletResponse response ) throws Exception {
		FileInfo fileinfo = (FileInfo) FormBeanUtil.formBean(request, FileInfo.class);
		String startrowStr = request.getParameter("startrow");
		String maxrowStr = request.getParameter("maxrow");
		String search = request.getParameter("search");
		int startrow = startrowStr.matches("[0-9]*$")==true?Integer.parseInt(startrowStr) : -1;
		int maxrow = maxrowStr.matches("[0-9]*$")==true?Integer.parseInt(maxrowStr) : -1;
		List<FileInfo> list = null;
		if (search==null || search.equals("")) {
			list = upLoadAndDownLoadService.getFileInfoList(fileinfo, startrow, maxrow, 0);
		} else {
			list = upLoadAndDownLoadService.getFileInfoListBySearch(search, startrow, maxrow);
		}
		list = ItemFilterUtil.filterList(list, "is_del", 1);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("filelist", list);
		map.put("statelist", FileState.getList());
		response.setCharacterEncoding(SystemProperties.ENCODING);
		response.getWriter().print( JSONObject.wrap(map) );
		return null;
	}
	
	@RequestMapping("/download")
	public ModelAndView download( HttpServletRequest request, HttpServletResponse response ) throws Exception {
		String UUIDfilename = request.getParameter("UUIDfilename");
		String filename = request.getParameter("filename");
		if (UUIDfilename==null && filename==null) {
			return null;
		}
		response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(filename,"UTF-8"));
		int res = upLoadAndDownLoadService.download(UUIDfilename, filename, response);
		String state = DownloadState.getValue(res);
		if (state.equals( DownloadState.SUCCESS.getValue() )==false) {
			response.sendRedirect("/UpLoadAndDownLoadServlet/view/upload_download/error?error="+URLEncoder.encode(state, "UTF-8"));
		}
		return null;
	}

}
